package com.example.syd.threadtest;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

    private DownloadBinder mBinder = new DownloadBinder();
    class DownloadBinder extends Binder{
       public void startDownload(){
           Log.d(MyService.this.toString(),"startDownload");
       }
       public int getProgress(){
           Log.d(MyService.this.toString(),"getProgress executed");
           return 0;
       }
    }
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        return mBinder;
    }

    @Override
    //创建服务时候使用
    public void onCreate() {
        super.onCreate();
        Log.d(MyService.this.toString(),"onCreate service");
        Intent intent = new Intent(this,MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
        Notification notification = new Notification.Builder(this)
                .setContentTitle("this is content title")
                .setContentText("this is content text")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setContentIntent(pi)
                .build();
        startForeground(1,notification);
    }

    @Override
    //服务启动时候调用
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(MyService.this.toString(),"onStartCommand service");
        new Thread(new Runnable() {
            @Override
            public void run() {
                //处理逻辑
                stopSelf();
            }
        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    //服务销毁时候调用
    public void onDestroy() {
        Log.d(MyService.this.toString(),"onDestroy service");
        super.onDestroy();
    }
}
